import * as vscode from 'vscode';

export class ConfigurationManager {
    private static readonly CONFIG_SECTION = 'oh-yidian-tong';

    getServiceUrl(): string {
        return vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION)
            .get<string>('serviceUrl', 'http://localhost:8080');
    }

    getGlmApiKey(): string {
        return vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION)
            .get<string>('glmApiKey', 'df6af53dd93b4afe9c1f0781f198305a.zcW6GYuBCOz3Xb27');
    }

    isCodeCompletionEnabled(): boolean {
        return vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION)
            .get<boolean>('enableCodeCompletion', true);
    }

    isCodeGenerationEnabled(): boolean {
        return vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION)
            .get<boolean>('enableCodeGeneration', true);
    }

    isUnitTestGenerationEnabled(): boolean {
        return vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION)
            .get<boolean>('enableUnitTestGeneration', true);
    }

    isFuzzTestGenerationEnabled(): boolean {
        return vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION)
            .get<boolean>('enableFuzzTestGeneration', true);
    }

    getMaxCompletionResults(): number {
        return vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION)
            .get<number>('maxCompletionResults', 10);
    }

    getTimeout(): number {
        return vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION)
            .get<number>('timeout', 5000);
    }

    async updateServiceUrl(url: string): Promise<void> {
        const config = vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION);
        await config.update('serviceUrl', url, vscode.ConfigurationTarget.Global);
    }

    async updateGlmApiKey(key: string): Promise<void> {
        const config = vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION);
        await config.update('glmApiKey', key, vscode.ConfigurationTarget.Global);
    }

    async updateCodeCompletionEnabled(enabled: boolean): Promise<void> {
        const config = vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION);
        await config.update('enableCodeCompletion', enabled, vscode.ConfigurationTarget.Global);
    }

    async updateCodeGenerationEnabled(enabled: boolean): Promise<void> {
        const config = vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION);
        await config.update('enableCodeGeneration', enabled, vscode.ConfigurationTarget.Global);
    }

    async updateUnitTestGenerationEnabled(enabled: boolean): Promise<void> {
        const config = vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION);
        await config.update('enableUnitTestGeneration', enabled, vscode.ConfigurationTarget.Global);
    }

    async updateFuzzTestGenerationEnabled(enabled: boolean): Promise<void> {
        const config = vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION);
        await config.update('enableFuzzTestGeneration', enabled, vscode.ConfigurationTarget.Global);
    }

    async updateMaxCompletionResults(maxResults: number): Promise<void> {
        const config = vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION);
        await config.update('maxCompletionResults', maxResults, vscode.ConfigurationTarget.Global);
    }

    async updateTimeout(timeout: number): Promise<void> {
        const config = vscode.workspace.getConfiguration(ConfigurationManager.CONFIG_SECTION);
        await config.update('timeout', timeout, vscode.ConfigurationTarget.Global);
    }

    // 监听配置变化
    onConfigurationChanged(callback: () => void): vscode.Disposable {
        return vscode.workspace.onDidChangeConfiguration((event) => {
            if (event.affectsConfiguration(ConfigurationManager.CONFIG_SECTION)) {
                callback();
            }
        });
    }
}
